package cn.zswltech.gruul.web.api.controller;

import cn.hutool.core.util.StrUtil;
import cn.zswltech.gruul.api.UserCenterApi;
import cn.zswltech.gruul.biz.service.UserService;
import cn.zswltech.gruul.common.result.MSG;
import cn.zswltech.gruul.common.result.Response;
import cn.zswltech.gruul.common.entity.UserDO;
import cn.zswltech.gruul.common.dal.vo.UserVO;
import cn.zswltech.gruul.web.api.controller.common.BaseController;
import cn.zswltech.gruul.common.param.UserParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class UserCenterController extends BaseController implements UserCenterApi {

    @Resource(name = "userServiceAPI")
    private UserService userService;

    /**
     * 获取用户信息
     *
     * @return
     * @
     */
    @Override
    public Response<UserVO> getUserInfo() {
        return userService.getUserInfo();
    }

    /**
     * 用户修改自己的信息
     *
     * @return
     * @
     */
    @Override
    public Response updateUser(UserParam userParam) {
        UserDO userDO = UserParam.genUserDO(userParam);
        userDO.setId(userParam.getId());
        if (StrUtil.isNotEmpty(userDO.getPhone()) && userDO.getPhone().contains("****")) {
            userDO.setPhone(null);
        }
            return userService.updateUserInfo(userDO);
    }

    /**
     * 用户修改密码
     *
     * @param oldPwd
     * @param newPwd
     * @return
     * @
     */
    @Override
    public Response changePwd(@RequestParam(value = "oldPwd") String oldPwd,
                              @RequestParam(value = "newPwd") String newPwd) {
        return userService.changePwd(oldPwd, newPwd);
    }

    /**
     * 用户修改密码 —— 仅用于登录后强制修改密码，此时不需要输入原密码
     * @param newPwd
     * @return
     * @
     */
    @Override
    public Response changePwd(@RequestParam(value = "newPwd") String newPwd) {
        if (StringUtils.isBlank(newPwd)) {
            Response response = new Response();
            response.setMessage(MSG.req_error_param_null, "newPwd");
            return response;
        }
        return userService.changePwdAfterLogin(newPwd);
    }

}
